/**
 * @param {number[]} nums
 * @param {number} target
 * @return {number}
 * 1、暴力解法没啥意义
 * 2、
 */
var countPairs = function (nums, target) {
  nums.sort((a, b) => {
    return a - b
  })
  let res = 0
  for (let i = 0; i < nums.length - 1; i++) {
    for (let j = i + 1; j < nums.length; j++) {
      if (nums[i] + nums[j] < target) {
        res++
      }
      else
        break
    }
  }
  return res
}
var countPairs = function (nums, target) {
  nums.sort((a, b) => {
    return a - b
  })
  let res = 0, i = 0, len = nums.length
  while (i < len) {
    let j = len - 1
    while (i < j && nums[i] + nums[j] >= target) {
      j--
    }
    res += j - i
    i++
  }
  return res
}

// console.log(countPairs([-1, 1, 2, 3, 1], 2))
console.log(countPairs([-6, 2, 5, -2, -7, -1, 3], -2))